package com.ccx.coachsite.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.ccx.coachsite.R;

public abstract class CommonActivity extends Activity {
	protected Activity mContext;
	
	protected View mReturn; // 返回
	protected View mTopLayout; // top背景
	protected boolean mReturnEnable = true; // 默认显示
	// private LoadingProgress progress; // 暂保留

	private View mLoadingTip; // 进度栏
	private TextView mProgressTip; // 进度提示文字

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); // remove title

		setContentView(getMainContentViewId()); // set view

		mContext = this; // get context
		mTopLayout = this.findViewById(R.id.login_top);
		
		initComponents(); // init all components
		
		initData(); // init the whole activity's data

		setCustomTitleTop(); // set custom title top
		
		setReturnBtnEnable(); // set return button
		
		
	}
	
	/**
	 * 初始化UI组件及数据
	 */
	protected abstract void initComponents();

	protected abstract void initData();

	/**
	 * 布局ID
	 * @return
	 */
	protected abstract int getMainContentViewId();

	protected abstract Class<?> getResouceClass();

	/**
	 * 自定义应用Top
	 */
	protected abstract void setCustomTitleTop();

	public void showProgressBar() {
		// progress.show();
		if (mLoadingTip == null) {
			mLoadingTip = findViewById(R.id.login_loading_bar);
		}
		if (mLoadingTip != null) {
			mLoadingTip.setVisibility(View.VISIBLE);
		}
	}

	public void hideProgressBar() {
		// progress.hide();
		
		if (mLoadingTip == null) {
			mLoadingTip = findViewById(R.id.login_loading_bar);
		}
		if (mLoadingTip != null) {
			mLoadingTip.setVisibility(View.INVISIBLE);
		}
	}

	public void updateProgressTip(String tip) {
		if (mProgressTip == null) {
			View view = findViewById(R.id.login_progress_tip);
			if (view instanceof TextView)
				mProgressTip = (TextView) view;
		}

		if (mProgressTip != null) {
			mProgressTip.setText(tip);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// progress.dismiss();
	}

	public class OnReturnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);

		// overridePendingTransition(android.R.anim.slide_out_right,android.R.anim.slide_in_left);
		// //由右向左划开特效
	}

	public void setTopTitle(int stringId) {
		TopViewUtil.updateTitle(this, R.id.login_top_title, stringId);
	}

	public void setRightBtn(int stringId, int btnBgId) {
		TopViewUtil.updateRightTitle(this, R.id.login_top_right, stringId);
		TopViewUtil.updateRight(this, R.id.login_top_right, btnBgId);
	}

	/**
	 * 是否启用返回按钮
	 * @param enable
	 */
	public void setReturnBtnEnable(boolean enable) {
		if (!enable) {
			mReturnEnable = enable;
		}
	}

	/**
	* 设置返回键
	*/
	private void setReturnBtnEnable() {
		mReturn = findViewById(R.id.login_btn_return);
		if (mReturn != null) {
			if (!mReturnEnable) {
				mReturn.setVisibility(View.GONE);
			} else {
				mReturn.setVisibility(View.VISIBLE);
				mReturn.setOnClickListener(new OnReturnClickListener());
			}
		}
	}

}
